<?php
/**
 * Created by PhpStorm.
 * User: teddy
 * Date: 15-12-23
 * Time: 下午5:14
 */


/*判断Request方式，switch到自己需要的方法*/
$request_method = strtolower($_SERVER['REQUEST_METHOD']);

switch ($request_method) {
    case 'put':
        /*request方式为delete，传入json的格式为{"userName":"TeddyLong","address":"shanghai,China"}*/
        $raw_post_data = file_get_contents('php://input', 'r');
        $jsonEntity = json_decode($raw_post_data);
        $userName = $jsonEntity->userName;
        $address = $jsonEntity->address;
        if(updateUser($userName,$address) > 0)
        {
            echo "Update Success!";
        }
        else
        {
            echo "Update Fail!";
        }
        break;
    case 'post':
        echo "POST Method!";
        break;
    case 'get':
        echo "GET Method!";
        break;
    case 'delete':
        echo "Delete Method!";
        break;
    default:
        break;
}

function updateUser($username, $address)
{
    $mysqli = new mysqli('localhost','root','1','DTXMB');
    if (mysqli_connect_errno())
    {
        echo "{" . '"error"' . ":" . mysqli_connect_error() . "}";
    }
    $sql = "Update Custom SET address = '" . $address . "' " . "WHERE NAME = '" . $username . "'";
    $mysqli->query("set character set 'utf8'");
    $mysqli->query("set names 'utf8'");
    $ret = $mysqli->query($sql);
    $rc = $mysqli->affected_rows;
    return $rc;
}